﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGCourt.Logic.Common;
using KMFrame.Core;
using NBearLite;
using LGCourt.Logic.NBL;
using LGCourt.Logic.NBL.DBEntities;
using System.Data;

namespace LGCourt.Logic.BLL
{
    public class BBS : LGCourtLogic
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetCondition(InfoCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.vPortalArticleTitleEx.aid, 
                LGCourtColumns.vPortalArticleTitleEx.title,
                LGCourtColumns.vPortalArticleTitleEx.from,
                LGCourtColumns.vPortalArticleTitleEx.createart,
            };
            cdtColl.SelectColumns = selectColumns;

            vPortalCategory category = GetCategoryInfo(condition);

            WhereClip where = LGCourtColumns.vPortalArticleTitleEx.catid == (category == null ? 0 : category.catid);
            if (!string.IsNullOrEmpty(condition.from))
                where = where && LGCourtColumns.vPortalArticleTitleEx.from.Like(condition.from + "%");
            if (condition.IsPicNews)
                where = where && LGCourtColumns.vPortalArticleTitleEx.pic != "";
            if (condition.BegingDate.Year > 2000)
            {
                where = where && LGCourtColumns.vPortalArticleTitleEx.createart >= condition.BegingDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.vPortalArticleTitleEx.createart <= condition.EndDate;
            }
           
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vPortalArticleTitleEx)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vPortalArticleTitleEx.aid)
                .OrderBy(LGCourtColumns.vPortalArticleTitleEx.createart.Desc);

            return cdtColl;
        }

        private vPortalCategory GetCategoryInfo(InfoCondition condition)
        {           
            WhereClip where = LGCourtColumns.vPortalCategory.catname == condition.InfoTag;

            string DeptName = string.IsNullOrEmpty(condition.UpInfoTag) ? (string.IsNullOrEmpty(condition.from) ? "" : condition.from) : condition.UpInfoTag;
            if (!string.IsNullOrEmpty(DeptName))
            {
                vPortalCategory upCategory = NBLData.Select(LGCourtColumns.vPortalCategory)
               .Where(LGCourtColumns.vPortalCategory.catname == DeptName)
               .ToSingleObject<vPortalCategory>();
                if (upCategory != null)
                    where = where && LGCourtColumns.vPortalCategory.upid == upCategory.catid;
            }

            vPortalCategory category = NBLData.Select(LGCourtColumns.vPortalCategory)
               .Where(where)
               .ToSingleObject<vPortalCategory>();
            return category;
        }

        /// <summary>
        /// 资讯信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetNewsInfoPage(InfoCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetCondition(condition).Select.ToList<vPortalArticleTitleEx>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vPortalArticleTitleEx, LGCourtColumns.vPortalArticleTitleEx.aid, GetCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


        public IEnumerable<vPortalArticleTitleEx> GetNewsInfoTop(InfoCondition condition)
        {            
            return GetCondition(condition).Select.ToList<vPortalArticleTitleEx>(); ;
        }

        public vPortalArticleTitleEx GetInfo(InfoCondition condition)
        {
            return NBLData.Select(LGCourtColumns.vPortalArticleTitleEx)
                .Where(LGCourtColumns.vPortalArticleTitleEx.aid == condition.aid)
                .ToSingleObject<vPortalArticleTitleEx>();
        }

        public vPortalArticleContent GettNewsContentInfo(InfoCondition condition)
        {
            vPortalArticleContent result = new vPortalArticleContent();
            ExpressionClip[] zjjhColumns =
       new ExpressionClip[] { 
                LGCourtColumns.vPortalArticleContent.aid,
                LGCourtColumns.vPortalArticleContent.cid,
                LGCourtColumns.vPortalArticleContent.content,
                LGCourtColumns.vPortalArticleContent.dateline,
                LGCourtColumns.vPortalArticleContent.id,
                LGCourtColumns.vPortalArticleContent.title,
                LGCourtColumns.vPortalArticleContent.pageorder,
                LGCourtColumns.vPortalArticleContent.idtype,
            };
            //证据交换
            DataSet ds = NBLData.Select(LGCourtColumns.vPortalArticleContent)
           .Where(LGCourtColumns.vPortalArticleContent.aid == condition.aid)
           .ToDataSet();  //ToList<pZJJHB>().FirstOrDefault();
            if (ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                return null;
            DataRow dr = ds.Tables[0].Rows[0];
            result.aid = dr["aid"] == DBNull.Value ? (decimal)0 : decimal.Parse(dr["aid"].ToString());
            result.content = dr["content"].ToString();
            result.dateline = dr["dateline"] == DBNull.Value ? (decimal)0 : decimal.Parse(dr["dateline"].ToString());
            result.title = dr["title"] == DBNull.Value ? "" : dr["title"].ToString();
            result.pageorder = dr["pageorder"] == DBNull.Value ? (decimal)0 : decimal.Parse(dr["pageorder"].ToString());
            result.idtype = dr["idtype"] == DBNull.Value ? "" : dr["idtype"].ToString();

            return result;
        }

        /// <summary>
        /// 获取博客条件
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetBlogCondition(InfoCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();

            WhereClip where = WhereClip.All;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vBlogHomeInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vBlogHomeInfo.blogid)
                .OrderBy(LGCourtColumns.vBlogHomeInfo.blogid.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 获取博客信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetBlogPage(InfoCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetBlogCondition(condition).Select.ToList<vBlogHomeInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vBlogHomeInfo, LGCourtColumns.vBlogHomeInfo.blogid, GetBlogCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public IEnumerable<vBlogHomeInfo> GetBlogTop(InfoCondition condition)
        {
            condition.PageSize = 5;
            return GetBlogCondition(condition).Select.ToList<vBlogHomeInfo>(); ;
        }

        /// <summary>
        /// 获取论坛信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetForumCondition(InfoCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();

            WhereClip where = WhereClip.All;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vForumTradeInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vForumTradeInfo.tid)
                .OrderBy(LGCourtColumns.vForumTradeInfo.tid.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 获取论坛信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetForumPage(InfoCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetForumCondition(condition).Select.ToList<vForumTradeInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vForumTradeInfo, LGCourtColumns.vForumTradeInfo.tid, GetForumCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public IEnumerable<vForumTradeInfo> GetForumTop(InfoCondition condition)
        {
            condition.PageSize = 5;
            return GetForumCondition(condition).Select.ToList<vForumTradeInfo>(); ;
        }
    }

    public class InfoCondition : PageParam
    {
        public string InfoTag { get; set; } //分类
        public string UpInfoTag { get; set; } //上一级分类
        public string from { get; set; }
        public int aid { get; set; } //信息ID
        public string keyword { get; set; }
        public DateTime BegingDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool IsPicNews { get; set; }
    }

    public class NewsInfoItem
    {
        public int aid { get; set; }
        public string title { get; set; }
        public string from { get; set; }
        public DateTime createart { get; set; }
    }
}
